package com.aishua.aishuawallet.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.utils.BitmapUtils;
import com.aishua.aishuawallet.utils.LogUtils;

/**
 * Created by aishua on 2018/8/2.
 */

public class RegistwoActivity extends BaseActivity {

    private static final String TAG = "RegistwoActivity";
    private Toolbar tl_regist;
    private ImageView img_registone;
    private ImageView img_registwo;
    private ImageView img_registhree;
    private TextView tv_registone;
    private TextView tv_registwo;
    private TextView tv_registhree;
    private EditText et_code;
    private TextView tv_countdowntimer;
    private Button btn_next;
    private String  phone;

    @Override
    public  void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registwo);
        initToolbar();
        phone = getIntent().getStringExtra("phone");
        LogUtils.v(TAG+"phone:"+phone);
        initView();
        initRegistNumview();
        business();
    }


    private void initView() {

        img_registone = findViewById(R.id.img_registone);
        img_registwo = findViewById(R.id.img_registwo);
        img_registhree = findViewById(R.id.img_registhree);
        tv_registone = findViewById(R.id.tv_registone);
        tv_registwo = findViewById(R.id.tv_registwo);
        tv_registhree = findViewById(R.id.tv_registhree);
        et_code = findViewById(R.id.et_code);
        tv_countdowntimer = findViewById(R.id.tv_countdowntimer);
        btn_next = findViewById(R.id.btn_next);

    }

    private void initRegistNumview() {

        int size = (int) getResources().getDimension(R.dimen.img_regist_num_size) * 2;
        LogUtils.v(TAG + "size:" + size);
        int backgroudselect = getResources().getColor(R.color.deepblue);
        int backgroudnoselect = getResources().getColor(R.color.gray);
        Bitmap bitmap1 = BitmapUtils.getNumberBitmap(size, "1", backgroudselect, Color.WHITE);
        img_registone.setImageBitmap(bitmap1);
        Bitmap bitmap2 = BitmapUtils.getNumberBitmap(size, "2", backgroudselect, Color.WHITE);
        img_registwo.setImageBitmap(bitmap2);
        Bitmap bitmap3 = BitmapUtils.getNumberBitmap(size, "3", backgroudnoselect, Color.WHITE);
        img_registhree.setImageBitmap(bitmap3);
        tv_registone.setTextColor(backgroudselect);
        tv_registwo.setTextColor(backgroudselect);
        tv_registhree.setTextColor(backgroudnoselect);


    }

    private void initToolbar() {

        tl_regist = findViewById(R.id.tl_regist);
        tl_regist.setTitle("");
        setSupportActionBar(tl_regist);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tl_regist.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

    }

    private void business() {

         CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {

                tv_countdowntimer.setText((millisUntilFinished / 1000) + "S");
            }

            @Override
            public void onFinish() {
                tv_countdowntimer.setEnabled(true);
                tv_countdowntimer.setText(getResources().getString(R.string.countdowntimer));
            }
        };
         timer.start();
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String code = et_code.getText().toString().trim();
                Intent intent = new Intent(RegistwoActivity.this,RegisthreeActivity.class);
                intent.putExtra("code",code);
                intent.putExtra("phone",phone);
                startActivity(intent);
            }
        });

        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.v(TAG + "s:" + s.toString() );
                if (null == s.toString() || "".equals(s.toString())) {

                    btn_next.setBackground(getResources().getDrawable(R.drawable.btn_no_click_auth));
                    btn_next.setEnabled(false);

                } else {

                    btn_next.setBackground(getResources().getDrawable(R.drawable.btn_click_auth));
                    btn_next.setEnabled(true);
                }
            }
            @Override
            public void afterTextChanged (Editable s){

            }
        });
    }


}
